library PetDies initializer init requires LegacySystem

	globals
		private trigger T
	endglobals
	private function PetCasualty takes nothing returns nothing
		local unit u = GetTriggerUnit()
		local player p = GetOwningPlayer(u)
		local integer i = GetConvertedPlayerId(p)
		local unit u2 = CreateUnit(p,'n012',11500.0,-3425.0,0.0)
		call DisplayTimedTextToPlayer(p,0.0,0.0,20.0,"|cffcd950cALICE|r : TacNet camera available.")
		
		//call TriggerRegisterUnitEvent(gg_trg_FPS_Target,u2,EVENT_UNIT_SPELL_EFFECT)
		//call TriggerRegisterUnitEvent(gg_trg_Activate_FPS,u2,EVENT_UNIT_SPELL_EFFECT)
		
		set udg_NightVision[i] = false
		call NightVision(p,udg_NightVision[i])
		call TriggerSleepAction(0.)
		call SelectUnitForPlayerSingle(u2,p)
		call ClearSelectionForPlayer(p)
		call TriggerSleepAction(0.)
		call SelectUnitForPlayerSingle(u2,p)
		set u = null
		set u2 = null
	endfunction

	function CreatePet takes player p, real x, real y returns nothing
		local integer i = GetConvertedPlayerId(p)
		local integer i2
		if udg_Experience[i]>120000 and GetRandomInt(0,2)==1 then
			set i2 = GetRandomInt(1,6)
		else
			set i2 = GetRandomInt(1,5)
		endif
	
	    set udg_Pet[i]=CreateUnit(p, udg_PetType[i2], x + 75.0, y - 75.0, 0.0)
        set udg_FPSUnit[i]=udg_Pet[i]
        call DisplayTextToPlayer(p, 0.0, 0.0, "Your spirit survives in the " + GetUnitName(udg_Pet[i]))
        call UnitAddItemById(udg_Pet[i], 'I00T')
        call TriggerSleepAction(0.)
        call SelectUnitForPlayerSingle(udg_Pet[i], p)
        call ClearSelectionForPlayer(p)
        call TriggerSleepAction(0.)
        call SelectUnitForPlayerSingle(udg_Pet[i], p)
        call TriggerRegisterUnitEvent(T, udg_Pet[i], EVENT_UNIT_DEATH)

        call UnitAddAbility(udg_Pet[i], 'A062')
	endfunction
	
	private function init takes nothing returns nothing
		set T = CreateTrigger()
		call TriggerAddAction(T,function PetCasualty)
	endfunction

endlibrary